package org.sparkle.html.impl.widget;

import java.util.List;
import java.util.UUID;

import org.sparkle.html.api.AbstractWidget;
import org.sparkle.html.impl.basic.Input;

public class AutoComplete extends AbstractWidget {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -2221382567335496017L;
	private Input input;
	
	public AutoComplete() {
		append(getInput());
	}
	
	public AutoComplete addValues(List<String> values){
		String value="[";
		for(String s: values){
			value+="\""+s+"\",";
		}
		if(value.endsWith(",")){
			value=value.substring(0, value.length()-1);
		}
		value+="]";
		getScriptAttribute().put("source", value);
		refreshScript();
		return this;
	}
	
	public Input getInput(){
		if(input==null)
			input=new Input(Input.TYPE_TEXT,UUID.randomUUID().toString(), "");
		return input;
	}

	@Override
	protected String getWidgetId() {
		return getInput().getId();
	}
}
